1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
import {join, normalize} from 'path'; |
4
|
|
|
import gulp from 'gulp'; |
5
|
|
|
import sourceMaps from 'gulp-sourcemaps'; |
6
|
|
|
import autoprefixer from 'gulp-autoprefixer'; |
7
|
|
|
import babel from 'gulp-babel'; |
8
|
|
|
import concat from 'gulp-concat'; |
9
|
|
|
import uglify from 'gulp-uglify'; |
10
|
|
|
import sass from 'gulp-sass'; |
11
|
|
|
import browserify from 'gulp-browserify'; |
12
|
|
|
import babelify from 'babelify'; |
13
|
|
|
|
14
|
|
|
export const ROOT_DIR = normalize(join(__dirname)); |
15
|
|
|
export const CONFIG = { |
16
|
|
|
JS: { |
17
|
|
|
INPUT: [ |
18
|
|
|
'app.js' |
19
|
|
|
], |
20
|
|
|
OUTPUT: 'app.js' |
21
|
|
|
}, |
22
|
|
|
DIR: { |
23
|
|
|
SRC: 'src', |
24
|
|
|
DIST: 'assets', |
25
|
|
|
JS: 'javascript', |
26
|
|
|
CSS: 'styles' |
27
|
|
|
}, |
28
|
|
|
BROWSERS: [ |
29
|
|
|
'ie >= 10', |
30
|
|
|
'ie_mob >= 10', |
31
|
|
|
'ff >= 30', |
32
|
|
|
'chrome >= 34', |
33
|
|
|
'safari >= 7', |
34
|
|
|
'opera >= 23', |
35
|
|
|
'ios >= 7', |
36
|
|
|
'android >= 4.4', |
37
|
|
|
'bb >= 10' |
38
|
|
|
] |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
export function javascript(source = [], output = '') { |
42
|
|
|
if (output == '' || source.length <= 0) return false; |
43
|
|
|
|
44
|
|
|
return gulp.src(source) |
45
|
|
|
.pipe(sourceMaps.init()) |
46
|
|
|
.pipe(browserify({ |
47
|
|
|
transform: ["babelify"], |
48
|
|
|
entries: source.map(item => { |
49
|
|
|
return join(ROOT_DIR, item); |
50
|
|
|
}), |
51
|
|
|
paths: [ |
52
|
|
|
join(ROOT_DIR, 'node_modules'), |
53
|
|
|
join(ROOT_DIR, CONFIG.DIR.SRC) |
54
|
|
|
] |
55
|
|
|
})) |
56
|
|
|
.pipe(babel({ |
57
|
|
|
presets: ['es2015'] |
58
|
|
|
})) |
59
|
|
|
.pipe(concat(output)) |
60
|
|
|
.pipe(uglify()) |
61
|
|
|
.pipe(sourceMaps.write('.')) |
62
|
|
|
.pipe(gulp.dest(`${CONFIG.DIR.DIST}/${CONFIG.DIR.JS}`)); |
63
|
|
|
}; |
64
|
|
|
export function styles() { |
65
|
|
|
return gulp.src(`${CONFIG.DIR.SRC}/${CONFIG.DIR.CSS}/**/*.{sass,scss}`) |
66
|
|
|
.pipe( |
67
|
|
|
sass({ |
68
|
|
|
outputStyle: 'compressed' |
69
|
|
|
}).on('error', sass.logError) |
70
|
|
|
) |
71
|
|
|
.pipe(autoprefixer(CONFIG.BROWSERS)) |
72
|
|
|
.pipe(gulp.dest(`${CONFIG.DIR.DIST}/${CONFIG.DIR.CSS}`)); |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
gulp.task('javascript', () => javascript(CONFIG.JS.INPUT.map(item => { |
76
|
|
|
return join(CONFIG.DIR.SRC, CONFIG.DIR.JS, item); |
77
|
|
|
}), CONFIG.JS.OUTPUT)); |
78
|
|
|
|
79
|
|
|
gulp.task('styles', () => styles()); |
80
|
|
|
|
81
|
|
|
gulp.task('default', ['javascript', 'styles']); |
82
|
|
|
|
83
|
|
|
gulp.task('watch', ['default', 'javascript', 'styles'], () => { |
84
|
|
|
gulp.watch(`${CONFIG.DIR.SRC}/${CONFIG.DIR.JS}/**/*.js`, ['javascript']); |
85
|
|
|
gulp.watch(`${CONFIG.DIR.SRC}/${CONFIG.DIR.CSS}/**/*.{sass,scss}`, ['styles']); |
86
|
|
|
}); |